package hjt.com.module_test.fragment;


import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Build;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.tong.lib.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import hjt.com.componentbase.constant.ARouterConstants;
import hjt.com.module_test.R;
import hjt.com.module_test.adpter.TestAdpter;
import hjt.com.module_test.databinding.FragmentRlvWebviewBinding;

/**
 * A simple {@link Fragment} subclass.
 */
@Route(path = ARouterConstants.FRAGMENT_TEST_RLVWEB)
public class RlvWebviewFragment extends BaseFragment {


    private FragmentRlvWebviewBinding mBinding;
    private List<String> dataList = new ArrayList<>();


    public RlvWebviewFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_rlv_webview, container, false);
            mView = mBinding.getRoot();
            init(savedInstanceState);
            initEvent();
        }
        return mView;
    }

    @Override
    protected int getLayoutResID() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        for (int i = 0; i < 20; i++) {
            dataList.add("test" + i);
        }
        mBinding.rlv.setLayoutManager(new LinearLayoutManager(getContext()));
        TestAdpter testAdpter = new TestAdpter(getContext(), dataList);
        mBinding.rlv.setAdapter(testAdpter);
        WebSettings webSetting = mBinding.webview.getSettings();
        webSetting.setJavaScriptEnabled(true);                          //支持Javascript 与js交互
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);      //支持通过JS打开新窗口
        webSetting.setAllowFileAccess(true);                            //设置可以访问文件
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setSupportZoom(true);                                //支持缩放
        webSetting.setBuiltInZoomControls(true);                        //设置内置的缩放控件
        webSetting.setUseWideViewPort(true);                            //自适应屏幕
        webSetting.setSupportMultipleWindows(true);                     //多窗口
        webSetting.setDefaultTextEncodingName("utf-8");                 //设置编码格式
        webSetting.setAppCacheEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);             //缓存模式
        webSetting.setUserAgentString(webSetting.getUserAgentString());//设置用户代理
        if (Build.VERSION.SDK_INT >= 21) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mBinding.webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view.loadUrl(request.getUrl().toString());
                } else {
                    view.loadUrl(request.toString());
                }
                return true;
            }
        });
        mBinding.webview.setWebChromeClient(new WebChromeClient());
        mBinding.webview.loadUrl("https://mp.weixin.qq.com/s/ei-2ezkCAkRAmbiMycHx6w");

        mBinding.nsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView nestedScrollView, int i, int i1, int i2, int i3) {
                Log.e("height",mBinding.webview.getMeasuredHeight()+"");
            }
        });
    }

    @Override
    protected void initEvent() {

    }

}
